﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Model.Entities;

namespace Model
{
    class CollectionProvider : DataAccess
    {
        protected AlbumDetails GetCollectionFromReader(IDataReader reader)
        {
            var album = new AlbumDetails((int)reader["AlbumID"], reader["AlbumName"].ToString(),
                                        (int)reader["AlbumYear"], reader["Style"].ToString(),
                                        (int)reader["ArtistID"], reader["ArtistName"].ToString(),
                                        reader["Cover"].ToString());
            GetTracksForAlbum(album);
            return album;
        }

        public void GetTracksForAlbum(AlbumDetails album)
        {
            var trackProvider = new TrackProvider();
            var tracks = trackProvider.GetTracksByAlbumId(album.AlbumId);
            foreach (var tr in tracks)
            {
                album.Tracks.Add(tr);
            }
        }

        public List<AlbumDetails> GetCollections()
        {
            var con = new SqlConnection(ConnectionString);
            var cmd = new SqlCommand("GetAlbums", con) { CommandType = CommandType.StoredProcedure };
            var albums = new List<AlbumDetails>();

            con.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            if (reader != null)
            {
                while (reader.Read())
                {
                    albums.Add(GetCollectionFromReader(reader));
                }
                reader.Close();
            }
            con.Close();
            return albums;
        }
    }
}
